﻿Public Class Frm_SelectElement

    Private Sub Frm_SelectElement_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Loadcomb()
        Loadcomparedmethod()
    End Sub
    Private Sub Loadcomparedmethod()
        Me.Cmbcompared.Items.Clear()
        Me.Cmbcompared.Items.Add("")
        For Each name As String In System.Enum.GetNames(GetType(Comparedmethod))
            Me.Cmbcompared.Items.Add(name)
        Next

        '下面的方法也可以
        'For Each field As System.Reflection.FieldInfo In (GetType(Comparedmethod)).GetFields()
        '    If field.FieldType.IsEnum Then
        '        Me.Cmbcompared.Items.Add(field.Name)
        '    End If
        'Next
        Me.Cmbcompared.Text = ""
    End Sub


    Public ElementSelected As String = String.Empty

#Region "Sub and Function"
    ''' <summary>
    ''' 載入倉庫
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Loadcomb()
        Try
            Dim StorhouseSQL As String = "select fcode,fname from BSC_StoreHouse"
            Dim StorhouseTable As DataTable = ReturnDataTable(StorhouseSQL)
            Me.CmbStorehouse.DataSource = StorhouseTable
            Me.CmbStorehouse.ValueMember = "fcode"
            Me.CmbStorehouse.DisplayMember = "fname"
            Me.CmbStorehouse.Text = ""
            Me.Cmbcompared.SelectedIndex = -1
        Catch ex As Exception

        End Try
    End Sub
#End Region

    Private Sub BtnQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnQuery.Click
        Dim fstorhouse As String = Me.CmbStorehouse.SelectedValue
        Dim fbarcode As String = Me.Txtbarcode.Text.Trim
        Dim felement As String = Me.Txtelement.Text.Trim
        Dim fplace As String = Me.TxtStoreplace.Text.Trim
        Dim QuerySQL As String = " select a.storehouse, a.barcode, a.element, a.qty, a.unit, a.elementbatch, a.place, a.status from Stor_elmentstatus a " + vbNewLine
        QuerySQL += "left join BSC_Goods  b on Replace(a.BarCode,'*','')=Replace(b.BarCode,'*','') " + vbNewLine
        QuerySQL += " where 1=1" & vbCrLf

        If Me.CmbStorehouse.Text.Length > 0 Then
            QuerySQL &= " and a.storehouse='" & fstorhouse & "'" & vbCrLf
        End If
        If Me.Txtbarcode.Text.Length > 0 Then
            QuerySQL &= " and Replace(a.barcode,'*','')='" & fbarcode.Replace("*", "") & "'" & vbCrLf
        End If
        If Me.Txtelement.Text.Length > 0 Then
            QuerySQL &= " and a.element='" & felement & "'" & vbCrLf
        End If
        If Me.TxtStoreplace.Text.Length > 0 Then
            QuerySQL &= " and a.place='" & fplace & "'" & vbCrLf
        End If
        If Me.txtEnname.Text.Trim.Length > 0 Then
            QuerySQL &= " and b.Enname='" + Me.txtEnname.Text.Trim.Replace("*", "") + "'" + vbNewLine
        End If
        If IsNumeric(Me.TxtQty1.Text.Trim) Then
            Select Case Me.Cmbcompared.Text.Trim
                Case Comparedmethod.大於等於.ToString()
                    QuerySQL &= " and Qty  >= " + Me.TxtQty1.Text.Trim + " "
                Case Comparedmethod.大於.ToString()
                    QuerySQL &= " and Qty  > " + Me.TxtQty1.Text.Trim + " "
                Case Comparedmethod.小於等於.ToString
                    QuerySQL &= " and Qty  <= " + Me.TxtQty1.Text.Trim + " "
                Case Comparedmethod.小於.ToString
                    QuerySQL &= " and Qty  < " + Me.TxtQty1.Text.Trim + " "
                Case Comparedmethod.不等於.ToString
                    QuerySQL &= " and Qty  <> " + Me.TxtQty1.Text.Trim + " "
                Case Comparedmethod.等於.ToString
                    QuerySQL &= " and Qty  = " + Me.TxtQty1.Text.Trim + " "
                Case Comparedmethod.在之間.ToString
                    If IsNumeric(Me.TxtQty2.Text.Trim) Then
                        QuerySQL &= " and Qty between " + Me.TxtQty1.Text.Trim + " and " + Me.TxtQty2.Text.Trim + " "
                    End If
                Case Comparedmethod.不在之間.ToString
                    If IsNumeric(Me.TxtQty2.Text.Trim) Then
                        QuerySQL &= " and Qty not between " + Me.TxtQty1.Text.Trim + " and " + Me.TxtQty2.Text.Trim + " "
                    End If
            End Select
        End If

        Me.DataGridView1.DataSource = Exec(QuerySQL, E_ExecType.DataTable)
    End Sub

    Private Sub DataGridView1_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If e.RowIndex <> -1 Then
            Me.ElementSelected = Me.DataGridView1.CurrentRow.Cells("Element").Value.ToString()
            Me.DialogResult = Windows.Forms.DialogResult.OK
        Else
            Me.DialogResult = Windows.Forms.DialogResult.None
        End If
    End Sub

    Private Sub Cmbcompared_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmbcompared.TextChanged
        If String.IsNullOrEmpty(Cmbcompared.Text.Trim) Then
            Me.TxtQty1.Enabled = False
            Me.TxtQty1.Text = ""
            Me.TxtQty2.Text = ""
            Me.TxtQty2.Enabled = False
            Exit Sub
        End If
        Select Case Cmbcompared.Text.Trim
            Case Comparedmethod.在之間.ToString()
                Me.TxtQty1.Enabled = True
                Me.TxtQty1.Focus()
                Me.TxtQty2.Enabled = True
            Case Comparedmethod.不在之間.ToString
                Me.TxtQty1.Enabled = True
                Me.TxtQty1.Focus()
                Me.TxtQty2.Enabled = True
            Case Else
                Me.TxtQty1.Enabled = True
                Me.TxtQty1.Focus()
                Me.TxtQty2.Enabled = False
                Me.TxtQty2.Text = ""
        End Select
    End Sub
End Class